package com.baidu.bos.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.baidu.bos.domain.User;
import com.baidu.bos.domain.Workordermanage;
import com.baidu.bos.service.IWorkordermanageService;
import com.baidu.bos.utils.BOSContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class TaskAction extends ActionSupport {
	@Resource
	private ProcessEngine processEngine;
	@Resource
	private IWorkordermanageService workordermanageService;
	private String taskId;

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	// 查询组任务
	public String findGroupTask() {
		TaskQuery query = processEngine.getTaskService().createTaskQuery();
		// 获取当前登录的用户
		User user = BOSContext.getLoginUser();
		// 过滤组任务
		query.taskCandidateUser(user.getId());
		List<Task> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "groupList";
	}
	
	// 查询个人任务
	public String findPersonalTask() {
		TaskQuery query = processEngine.getTaskService().createTaskQuery();
		// 获取当前登录的用户
		User user = BOSContext.getLoginUser();
		// 过滤组任务
		query.taskAssignee(user.getId());
		List<Task> list = query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "personalList";
	}

	// 显示数据
	public String showData() throws IOException {
		// 根据任务id获取流程变量
		Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);
		ServletActionContext.getResponse().setContentType("html/text;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(variables);
		return NONE;
	}

	// 拾取组任务
	public String takeTask() {
		String userId = BOSContext.getLoginUser().getId();
		processEngine.getTaskService().claim(taskId, userId);
		return "toGroupList";
	}
	
	//审核工作单
	public String checkWorkOrderManage(){
		//获取流程变量
		Map<String, Object> map = processEngine.getTaskService().getVariables(taskId);
		Workordermanage workordermanage = null;
		if (check == null) {
			//证明未审核,跳转到审核页面
			ActionContext.getContext().getValueStack().set("map", map);
			return "toCheck";
		}else {
			//将workordermanage中managecheck设置为"1",代表已经审核
			workordermanage = (Workordermanage) map.get("业务数据");
			workordermanage.setManagerCheck("1");
			//重新设置流程变量
			processEngine.getTaskService().setVariable(taskId, "业务数据", workordermanage);
			//已经审核,将check的值设置为流程变量,并办理流程
			map.put("check", check);
			processEngine.getTaskService().complete(taskId, map );
			if (check.equals("0")) {
				//证明审核未通过,将流程实例设置为未启动的状态
				workordermanage.setStart("0");
			}
			//修改到数据库
			workordermanageService.update(workordermanage);
		}
		return "toPersonalList";
	}
	//货物出库
	public String outStore(){
		processEngine.getTaskService().complete(taskId);
		return "toPersonalList";
	}
	//货物配送
	public String transferGoods(){
		processEngine.getTaskService().complete(taskId);
		return "toPersonalList";
	}
	//配送签收
	public String receive(){
		processEngine.getTaskService().complete(taskId);
		return "toPersonalList";
	}
	
	
	
	private String check;

	public void setCheck(String check) {
		this.check = check;
	}

}
